﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Base;
using MLM.Data;
using MLM.AdminCP.Class;

namespace MLM.AdminCP.Pages
{
    public partial class Products : BasePage
    {
        public MLMDataContext dtct = new MLMDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HfUserID.Text = Permission.GetCurrentUserId().ToString();
                HfCountryID.Text = "1";
                using (var DataContext = new MLMDataContext())
                {
                    //long x = long.Parse(Permission.GetCurrentRole().ToString());
                    //if (x == 2)
                    //{
                    //    var query = DataContext.ProductBases.Include("ProductCategory").ToList();
                    //    lvDepoProduct.DataSource = query;
                    //    lvDepoProduct.DataBind();
                    //}
                }
            }
            CurrentRoleID.Value = (Permission.GetCurrentRole() - 1).ToString();
            RabCountry.SelectedIndexChanged += new Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventHandler(RabCountry_SelectedIndexChanged);
            RadUser.SelectedIndexChanged += new Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventHandler(RadUser_SelectedIndexChanged);
            try
            {
                BindData();
            }
            catch (Exception)
            {

            }   
        }

        void RadUser_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
           // BindData();
        }
        protected void DataPagerDepo_PreRender(object sender, EventArgs e)
        {
            BindData();
        }

        protected void DataPager_PreRender(object sender, EventArgs e)
        {
            //BindData();
        }
        protected void ProductViews_OnPagePropertiesChanged(object sender, EventArgs e)
        {
            ProductViews.DataBind();
        }
        protected void lvDepoProduct_PagePropertiesChanged(object sender, EventArgs e)
        {
            lvDepoProduct.DataBind();
        }
        void RabCountry_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadUser.Text = "";
            RadUser.ClearSelection();
            lvDepoProduct.DataSource = null;
            lvDepoProduct.DataBind();
            ProductViews.DataSource = null;
            ProductViews.DataBind();
            HfCountryID.Text = RabCountry.SelectedValue;
        }

        /// <summary>
        /// price in shop
        /// </summary>
        /// <param name="price"></param>
        /// <param name="tax"></param>
        /// <returns> pricetax= price*tax ? try now ? did you upload this code ?// uploaded
        /// </returns>
        public static string FormatPrice(object price, object tax, MLMDataContext ct, object productid = null, object countryid = null)
        {
            try
            {
                if (productid != null && countryid != null && countryid != "" && productid != "")
                {
                    long Role = Permission.GetCurrentRole();
                    long pId = long.Parse(productid.ToString());
                    long cId = long.Parse(countryid.ToString());
                    var pPrice =
                        ct.ProductPrices.FirstOrDefault(
                            it => it.ProductBaseID == pId && it.CountryID == cId && it.RoleID == Role);
                    if (pPrice != null)
                    {
                        decimal _price = decimal.Parse(pPrice.Price.ToString());
                        decimal _tax = decimal.Parse(tax.ToString());
                        return (NumberUtil.Round(_price + _price * (_tax / 100))).ToString() + " &euro;";
                    }
                    else
                    {
                        decimal _price = decimal.Parse(price.ToString());
                        decimal _tax = decimal.Parse(tax.ToString());
                        return (NumberUtil.Round(_price + _price * (_tax / 100))).ToString() + " &euro;";
                    }
                }
                else
                {
                    decimal _price = decimal.Parse(price.ToString());
                    decimal _tax = decimal.Parse(tax.ToString());
                    return (NumberUtil.Round(_price + _price * (_tax / 100))).ToString() + " &euro;";
                }
            }
            catch (Exception ex)
            {
                return "0";
            }
        }
        public void BindData()
        {
            try
            {
                HfUserID.Text = RadUser.SelectedValue;
                using (var DataContext = new MLMDataContext())
                {
                    long x = long.Parse(Permission.GetCurrentRole().ToString());
                    if (x == 2)
                    {
                        var query = DataContext.ProductBases.Include("ProductCategory").ToList();
                        lvDepoProduct.DataSource = query;
                        lvDepoProduct.DataBind(); //thang nay role =2
                    }
                    else
                    {
                        long y = long.Parse(RadUser.SelectedValue);
                        var selectedUserRole = DataContext.Users.FirstOrDefault(it => it.UserId == y).RoleID;
                        if (selectedUserRole == 1)
                        {
                            var query = DataContext.ProductBases.Where(it => it.StockAvailable > 0).ToList();
                            lvDepoProduct.DataSource = query;
                            lvDepoProduct.DataBind();
                            lvDepoProduct.Visible = true;
                            ProductViews.Visible = false;
                        }
                        else
                        {
                            var query =
                                DataContext.UserStocks.Include("ProductBas").Include("ProductBas.ProductCategory").Where(
                                    p => p.UserID == y && p.StockAvaiable > 0).ToList();
                            ProductViews.DataSource = query;
                            ProductViews.DataBind();
                            lvDepoProduct.Visible = false;
                            ProductViews.Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Response.Write(ex.Message);
            }
        }
        //
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetProductIdByPNumber(object _productnumber, object _depoid)
        {
            string pNumber = _productnumber.ToString();
            long? _depoId = 0;
            try
            {
                _depoId = long.Parse(_depoid.ToString());
            }
            catch (Exception)
            {
                return Resources.MLM.WrongDepoId;
            }
            MLMDataContext ct = new MLMDataContext();
            long? productId = 0;
            try
            {
                var depo = ct.Users.FirstOrDefault(it => it.UserId == _depoId);
                if (depo != null)
                {
                    if (depo.RoleID == 1)
                    {
                        productId = ct.ProductBases.FirstOrDefault(it => it.ProductNumber.ToLower() == pNumber.ToLower()).ProductBaseID;
                    }
                    else
                    {
                        var product = ct.ProductBases.FirstOrDefault(it => it.ProductNumber.ToLower() == pNumber.ToLower()).ProductBaseID;
                        productId =
                            ct.UserStocks.FirstOrDefault(it => it.UserID == _depoId && it.StockAvaiable > 0 && it.ProductBaseID == product).
                                ProductBaseID;
                    }
                }
                return productId.ToString();
            }
            catch (Exception)
            {
                return "0";
            }
        }
    }
}